package com.clz.hyperit.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.clz.hyperit.dao.UserDetailDao;
import com.clz.hyperit.entity.Article;
import com.clz.hyperit.entity.User;
import com.clz.hyperit.entity.UserDetail;
import com.clz.hyperit.vo.PageInfo;

@Repository("userDetailDao")
public class UserDetailDaoImpl extends BaseDaoImpl<UserDetail> implements
		UserDetailDao {

	public UserDetail getByUserId(Integer userId) {
		String hql = "from UserDetail where user_id = :userId";
		UserDetail userDetail = (UserDetail) getCurrentSession()
				.createQuery(hql).setInteger("userId", userId).uniqueResult();
		return userDetail;
	}

	public List<UserDetail> findAllByArticle(PageInfo pageInfo) {
		String hql = "from UserDetail  order by article desc";
		@SuppressWarnings("unchecked")
		List<UserDetail> list = getCurrentSession()
				.createQuery(hql)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from UserDetail ";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<UserDetail> findAllByFans(PageInfo pageInfo) {
		String hql = "from UserDetail  order by fans desc";
		@SuppressWarnings("unchecked")
		List<UserDetail> list = getCurrentSession()
				.createQuery(hql)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from UserDetail ";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<UserDetail> findAllByExp(PageInfo pageInfo) {
		String hql = "from UserDetail  order by exp desc";
		@SuppressWarnings("unchecked")
		List<UserDetail> list = getCurrentSession()
				.createQuery(hql)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from UserDetail ";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<UserDetail> findUdByNote(PageInfo pageInfo) {
		String hql = "from UserDetail  order by note desc";
		@SuppressWarnings("unchecked")
		List<UserDetail> list = getCurrentSession()
				.createQuery(hql)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from UserDetail ";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<UserDetail> findUdByAccept(PageInfo pageInfo) {
		String hql = "from UserDetail  order by accept desc";
		@SuppressWarnings("unchecked")
		List<UserDetail> list = getCurrentSession()
				.createQuery(hql)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from UserDetail ";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<UserDetail> searchBlog(PageInfo pageInfo, String search) {
		String hql = "select u from UserDetail u,Position p where (u.position_id = p.id) and (u.name like :search1 or u.city like :search2 or p.name like :search3)  order by u.createtime desc";
		// "select c from Club c, School s ,ClubType t where (c.schoolId = s.id and c.typeId = t.id)  and (c.name like :search1 or s.name like :search2 or t.name like :search3)";
		@SuppressWarnings("unchecked")
		List<UserDetail> list1 = getCurrentSession()
				.createQuery(hql)
				.setString("search1","%"+search+"%")
				.setString("search2","%"+search+"%")
				.setString("search3","%"+search+"%")
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select u from UserDetail u,Position p where (u.position_id = p.id) and (u.name like :search1 or u.city like :search2 or p.name like :search3)";
		@SuppressWarnings("unchecked")
		List<UserDetail> list2 = getCurrentSession()
				.createQuery(hql2)
				.setString("search1","%"+search+"%")
				.setString("search2","%"+search+"%")
				.setString("search3","%"+search+"%")
				.list();
		pageInfo.setTotalCount((long)list2.size());
		return list1;
	}

	public List<UserDetail> searchBlogByPosition(PageInfo pageInfo,
			Integer position_id) {
		String hql = "from UserDetail  where position_id = :position_id order by accept desc";
		@SuppressWarnings("unchecked")
		List<UserDetail> list = getCurrentSession()
				.createQuery(hql)
				.setInteger("position_id",position_id)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from UserDetail where position_id = :position_id";
		Object uniqueResult = getCurrentSession().createQuery(hql2).setInteger("position_id",position_id)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}
}
